package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.activity.toolstask.ToolsWedTaskAddtaskActivity;
import a66weding.com.jiehuntong.common.utils.CUtils;

/**
 * 自定义Adapter
 *
 * @author ldx
 */
public class Tools_ExpandableListViewAdapter extends BaseExpandableListAdapter implements View.OnClickListener {
    public String[] group = {"确定结婚", "婚宴场所", "婚纱摄影", "婚庆事项", "婚品购置"};
    public String[][] listViewChild = {
            {"双方父母见面商议结婚相关事宜", "确定结婚日期和地点", "商议婚礼开支分配", "办理结婚登记手续", "选购钻戒对戒"},
            {"疯狂恶搞", "搞笑综艺", "原创搞笑", "爆笑宠物", "雷人囧事"},
            {"原创热点", "原创影视", "音乐动画", "火星搞笑", "校园作品", "网络红人", "拍客"},
            {"社会资讯", "国内资讯", "国际资讯", "财富资讯", "科技资讯"},
            {"篮球天地", "足球世界", "综合体育", "极限运动", "武术摔角", "美女花边"},};
    String[][] child = {{""}, {""}, {""}, {""}, {""}};
    LayoutInflater mInflater;
    ToolsWedTaskAddtaskActivity mAddTaskActivity;
    ViewChild mViewChild;
    private ExpandableListView mExpandLview;


    public Tools_ExpandableListViewAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        this.mAddTaskActivity = (ToolsWedTaskAddtaskActivity) context;
        mExpandLview=mAddTaskActivity.getmExpandableListView();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return child[groupPosition][childPosition];
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }


    private void setListViewListener(ListView listView) {
        listView.setOnItemClickListener(new ListView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (view instanceof TextView) {
                    // 如果想要获取到哪一行，则自定义gridview的adapter，item设置2个textview一个隐藏设置id，显示哪一行
                    TextView tv = (TextView) view;
                    CUtils.showToast("我被电击了");
                    Toast.makeText(mAddTaskActivity, "position=" + position + "||" + tv.getText(), Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    /**
     * 设置gridview数据
     *
     * @param data
     * @return
     */
    private ArrayList<HashMap<String, Object>> setListViewData(String[] data) {
        ArrayList<HashMap<String, Object>> gridItem = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < data.length; i++) {
            HashMap<String, Object> hashMap = new HashMap<String, Object>();
            hashMap.put("channel_listview_item", data[i]);
            gridItem.add(hashMap);
        }
        return gridItem;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return child[groupPosition].length;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group[groupPosition];
    }

    @Override
    public int getGroupCount() {
        return group.length;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            mViewChild = new ViewChild();
            convertView = mInflater.inflate(R.layout.activity_tools_expandablelistview, null);
            mViewChild.textView = (TextView) convertView.findViewById(R.id.channel_group_name);
            mViewChild.imageView = (ImageView) convertView.findViewById(R.id.channel_imageview_orientation);
            convertView.setTag(mViewChild);
        } else {
            mViewChild = (ViewChild) convertView.getTag();
        }
        mViewChild.imageView.setTag(groupPosition);
        mViewChild.imageView.setOnClickListener(Tools_ExpandableListViewAdapter.this);

        if (isExpanded) {
            mViewChild.imageView.setImageResource(R.mipmap.shou);
        } else {
            mViewChild.imageView.setImageResource(R.mipmap.fang);
        }
        mViewChild.textView.setText(getGroup(groupPosition).toString());
        return convertView;
    }




    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            mViewChild = new ViewChild();
            convertView = mInflater.inflate(R.layout.activity_tools_expandablelistview_item, null);
            mViewChild.listView = (ListView) convertView.findViewById(R.id.channel_item_child_listView);
            convertView.setTag(mViewChild);
        } else {
            mViewChild = (ViewChild) convertView.getTag();
        }

        SimpleAdapter mSimpleAdapter = new SimpleAdapter(mAddTaskActivity,
                setListViewData(listViewChild[groupPosition]),
                R.layout.activity_tools_child_listview_item, new String[]{"channel_listview_item"},
                new int[]{R.id.channel_listview_item});
        mViewChild.listView.setAdapter(mSimpleAdapter);
        setListViewListener(mViewChild.listView);
        mViewChild.listView.setSelector(new ColorDrawable(Color.TRANSPARENT));


        return convertView;

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
                case R.id.channel_imageview_orientation:
                    int groupposition = (int) view.getTag();
                    if(mExpandLview.isGroupExpanded(groupposition)) {
                        mExpandLview.collapseGroup(groupposition);
                    }else{
                        mExpandLview.expandGroup(groupposition);


                    }
                break;

        }

    }


    static class ViewChild {
        ImageView imageView;
        TextView textView;
        ListView listView;

    }
}